package com.ldzysoft.myweather.utils;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.ldzysoft.myweather.db.City;
import com.ldzysoft.myweather.db.County;
import com.ldzysoft.myweather.db.Province;
import com.ldzysoft.myweather.gson.Weather;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonUtil {

    /*
    * 处理省级数据
    * */
    public static boolean handleProvinceData(String provinceData) {
        if (!TextUtils.isEmpty(provinceData)) {
            try {
                JSONArray allProvinces = new JSONArray(provinceData);
                for (int i = 0; i < allProvinces.length(); i++) {
                    JSONObject provinceObj = allProvinces.getJSONObject(i);
                    Province province = new Province();
                    province.setProvinceCode(provinceObj.getInt("id"));
                    province.setProvinceName(provinceObj.getString("name"));
                    province.save();  // 保存到数据库中
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /*
     * 处理市级数据
     * provinceId：数据库表的自增ID
     * */
    public static boolean handleCityData(String cityData, int provinceId) {
        if (!TextUtils.isEmpty(cityData)) {
            try {
                JSONArray allCities = new JSONArray(cityData);
                for (int i = 0; i < allCities.length(); i++) {
                    JSONObject cityObj = allCities.getJSONObject(i);
                    City city = new City();
                    city.setCityCode(cityObj.getInt("id"));
                    city.setCityName(cityObj.getString("name"));
                    city.setProvinceId(provinceId);
                    city.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /*
     * 处理县级数据
     * */
    public static boolean handleCountyData(String countyData, int cityId) {
        if (!TextUtils.isEmpty(countyData)) {
            try {
                JSONArray allCounties = new JSONArray(countyData);
                for (int i = 0; i < allCounties.length(); i++) {
                    JSONObject countyObj = allCounties.getJSONObject(i);
                    County county = new County();
                    county.setCountyName(countyObj.getString("name"));
                    county.setWeatherId(countyObj.getString("weather_id"));
                    county.setCityId(cityId);
                    county.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /*
    * 处理天气数据
    * */
    public static Weather handleWeatherData(String weatherData) {
        try {
            JSONObject weatherObj = new JSONObject(weatherData);
            JSONArray weatherArray = weatherObj.getJSONArray("HeWeather");
            String weatherContent = weatherArray.getJSONObject(0).toString();
            return new Gson().fromJson(weatherContent, Weather.class);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

}
